<template>
  <el-card shadow="never" class="aui-card--fill">
    <el-radio-group v-for="type in m_ltPageType" :key="type.value" style="float: right" v-model="m_nPageType" @change="switchType">
      <el-radio-button :label="type.value">{{type.name}}</el-radio-button>
    </el-radio-group>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.houseId" placeholder="猪舍" clearable>
          <el-option
              v-for="item in houses"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item prop="outType">
            <RenSelect dictType="leave_type" placeholder="销售类型" v-model="dataForm.outType"></RenSelect>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('sales:salesbreed:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('sales:salesbreed:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form-item>
    </el-form>
    <div class="mod-stock__transferbreed}">
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column v-for="field in m_mapFields[m_nPageType]" :key="field.prop" sortable="custom" :prop="field.prop" :fixed="field.fixed" :label="field.label" header-align="center" align="center">
        </el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('stock:transferbreed:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('stock:transferbreed:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <!--<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>-->
      <growth-leave v-if="growthVisible" ref="refGrowthLeave" @refreshDataList="getDataList"></growth-leave>
      <breed-leave v-if="breedVisible" ref="refBreedLeave" @refreshDataList="getDataList"></breed-leave>
      <upload-growth v-if="uploadGrowthVisible" ref="refUploadGrowth"></upload-growth>
      <upload-breed v-if="uploadBreedVisible" ref="refUploadBreed"></upload-breed>
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import GrowthLeave from '@/views/modules/stock/growth/growth-leave'
import BreedLeave from '@/views/modules/stock/pig/breed-leave'
import Upload from '@/views/modules/oss/oss-uploadgrowth'
import AddOrUpdate from './salesbreed-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      model: 'salebreed',
      m_ltPageType: [{ name: '种猪', value: 0 }, { name: '生长猪', value: 1 }],
      m_nPageType: 0,
      m_mapFields: {
        0: [{ prop: 'houseName', label: '猪舍', fixed: 'left', readonly: true }, { prop: 'grid', label: '栏位', fixed: 'left', readonly: true }, { prop: 'earmark', label: '耳号', fixed: 'left', required: true, search: true }, { prop: 'outDate', label: '销售日期', fixed: 'left', required: true, search: true }, { prop: 'outType', label: '销售类型', required: true, search: true }, { prop: 'leaveReason', label: '离场原因', required: true, search: true }, { prop: 'remark', label: '备注' }, { prop: 'creator', label: '创建者', readonly: true }, { prop: 'createDate', label: '创建时间', readonly: true }],
        1: [{ prop: 'houseName', label: '猪舍', fixed: 'left', required: true }, { prop: 'grid', label: '栏位', fixed: 'left', readonly: true }, { prop: 'outDate', label: '销售日期', fixed: 'left', required: true, search: true }, { prop: 'outType', label: '销售类型', fixed: 'left', required: true, search: true }, { prop: 'mounts', label: '头数', required: true }, { prop: 'price', label: '头单价' }, { prop: 'weight', label: '重量' }, { prop: 'operator', label: '负责人' }, { prop: 'remark', label: '备注' }, { prop: 'creator', label: '创建者', readonly: true }, { prop: 'createDate', label: '创建时间', readonly: true }]
      },
      growthVisible: false,
      breedVisible: false,
      uploadGrowthVisible: false,
      uploadBreedVisible: false,
      addOrUpdateVisible: false,
      mixinViewModuleOptions: {
        getDataListURL: '/sales/salesbreed/page',
        getDataListIsPage: true,
        exportURL: '/sales/salesbreed/export',
        deleteURL: '/sales/salesbreed',
        deleteIsBatch: true
      },
      dataForm: {
        id: '',
        outType: 1
      }
    }
  },
  components: {
    'growth-leave': GrowthLeave,
    'breed-leave': BreedLeave,
    Upload,
    AddOrUpdate
  },
  methods: {
    switchType () {
      if (this.m_nPageType === 0) {
        this.model = 'salesbreed'
        this.mixinViewModuleOptions.getDataListURL = `/sales/${this.model}/page`
        this.mixinViewModuleOptions.exportURL = `/sales/${this.model}/export`
        this.mixinViewModuleOptions.deleteURL = `/sales/${this.model}`
      } else {
        this.model = 'salesgrowth'
        this.mixinViewModuleOptions.getDataListURL = `/sales/${this.model}/page`
        this.mixinViewModuleOptions.exportURL = `/sales/${this.model}/export`
        this.mixinViewModuleOptions.deleteURL = `/sales/${this.model}`
      }
      this.query()
    },
    leaveHandle (id) {
      if (this.m_nPageType === 0) {
        this.breedVisible = true
        this.$nextTick(() => {
          this.$refs.refBreedLeave.dataForm.id = id
          this.$refs.refBreedLeave.init()
        })
      } else {
        this.growthVisible = true
        this.$nextTick(() => {
          this.$refs.refGrowthLeave.dataForm.id = id
          this.$refs.refGrowthLeave.init()
        })
      }
    },
    showUpload () {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.upload.init()
      })
    }
  }
}
</script>
